<?php

use yii\helpers\Html;
use yii\widgets\Pjax;
use yii\helpers\Url;


/* @var $this yii\web\View */
/* @var $searchModel frontend\modules\resource\models\HyBookSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */

$this->title = '资源管理';
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="hy-book-index">
    <div class="hy-book-btn" style="margin: 10px 0;position: relative;">
        <button type="button" data-value="1" class="btn btn-resource  btn-primary btn-lg">图书类</button>
        <button type="button" data-value="2" class="btn btn-resource  btn-success btn-lg">期刊类</button>
        <button type="button" data-value="3" class="btn btn-resource  btn-info btn-lg">图片类</button>
        <button type="button" data-value="4" class="btn btn-resource  btn-warning btn-lg">音频类</button>
        <button type="button" data-value="5" class="btn btn-resource  btn-danger btn-lg">视频类</button>
        <div class="addBtn">
            <?= Html::button('导入数据', ['class' => 'btn btn-primary daoru']) ?>
            <?= Html::button('下载模板', ['class' => 'btn btn-primary muban']) ?>
        </div>
    </div>
    <?php Pjax::begin(['id' => 'centerView']); ?>
    <div class="searchBody">
        <?= $this->render('_search', ['model' => $searchModel]); ?>
        <!--功能按钮-->
        <div class="addBtn">
            <?= Html::a('新增资源', 'javascript:;', ['class' => 'btn btn-success create']) ?>
            <?= Html::a('批量删除', 'javascript:;', ['class' => 'btn btn-danger z-delete']) ?>
        </div>
    </div>

    <?= $this->render('_index_' . $type, ['model' => $searchModel, 'dataProvider' => $dataProvider]); ?>
    <?php Pjax::end(); ?>
</div>
<div id="open" style="display: none;">
    <div style="margin-top: 20px;padding:0 20px;">
        <input type="file" id="file" class="fileInput">
        <input type="hidden" id="file_value" value="">
    </div>
    <div style="margin-top: 10px;padding:0 20px;">
        <!--d导入类型-->
        <select id="r_type" class="form-control" name="" aria-required="true" style="width: 100%;">
            <option value="1">图书</option>
            <option value="2">期刊</option>
            <option value="3">图片</option>
            <option value="4">音频</option>
            <option value="5">视频</option>
        </select>
    </div>
    <div style="display: flex;justify-content: center;margin-top: 20px">
        <span id="start_daoru" class='btn btn-primary' type="button">开始导入</span>
    </div>
</div>
<!--增删改 操作-->
<?php echo $this->render('../../../common/views/base/_column', ['option' => ['c', 'u']]); ?>

<script>
    <?php $this->beginBlock('js_ok_remove') ?>
    let ck = <?=$type?>;
    let down = true;
    //资源类型
    $("button[data-value='" + ck + "']").addClass('checkedCate');
    //默认导入类型
    $("#r_type").find("option[value='" + ck + "']").attr("selected", true);
    //下载模板
    $(document).on('click', '.muban', function () {
        if (down) {
            down = !down;
            window.location.href = '/resource/resource/template?type=' + ck;
            down = !down;
        }
    });

    //切换类型
    $(document).on('click', '.btn-resource', function () {
        var id = $(this).attr('data-value');
        window.location.href = '/resource/resource?type=' + id;
    });

    //批量删除
    $(document).on('click', '.z-delete', function () {
        let id = allChecked();
        if (id) {
            layer.confirm('确认删除资源？', {icon: 3, title: ''}, function (index) {
                $.ajax({
                    url: '<?=Url::toRoute('batch-delete')?>',//'/resource/resource/upload-file'
                    type: 'POST',
                    data: {
                        id: JSON.stringify(id),
                        r_type: ck
                    },
                    dataType: "json",
                    success: function (msg) {

                        if (msg.status == 200) {
                            layer.msg(msg['msg'], {icon: 1, time: 1000}, () => {
                                $.pjax.reload({container: "#centerView"});
                            });
                        } else {
                            layer.msg(msg['msg'], {icon: 2, time: 1000});
                        }
                    },
                    error: function () {
                    }
                });
                layer.close(index);
            });
        } else {
            layer.msg('先选择记录');
        }
    });

    //导入数据
    $(document).on('click', '.daoru', function () {
        layer.open({
            type: 1,
            title: "资源导入",
            area: ["350px", "200px"],
            content: $('#open'),
        });
    });
    //上传
    $(document).on('change', '#file', function () {
        console.log('上传');
        var excel_file = $(this);
        var fd = new FormData();
        fd.append('excel', excel_file[0].files[0]);

        var up = layer.msg('上传中', {
            icon: 16
            , shade: 0.01
        });
        $.ajax({
            url: '<?=Url::toRoute('upload-file')?>',//'/resource/resource/upload-file'
            type: 'POST',
            data: fd,
            processData: false,
            contentType: false,
            dataType: "json",
            success: function (msg) {

                if (msg.status == 200) {
                    layer.msg(msg['msg'], {icon: 1, time: 1000}, function () {
                        $('#file_value').val(msg['data']);
                    });
                } else {
                    layer.msg(msg['msg'], {icon: 2, time: 1000});
                }
            },
            error: function () {
            },
            complete: function () {
                layer.close(up);
            }
        })
    });
    //开始导入
    $(document).on('click', '#start_daoru', function () {
        var file = $('#file_value').val();
        if (!file) {
            layer.msg('没有导入数据', {icon: 2});
            return false;
        }
        $.ajax({
            type: "POST",
            url: '<?=Url::toRoute('import-excel')?>',
            data: {
                file: file,
                r_type: $("#r_type").val(),
            },
            dataType: 'json'
        })
            .done((msg) => {
                if (msg['status'] == 200) {
                    layer.msg(msg['msg'], {icon: 1, time: 1000}, function () {
                        $.pjax.reload({container: "#centerView"});
                        layer.closeAll();
                    });
                } else {
                    layer.msg(msg['msg'], {icon: 2});
                }
            })
            .fail((XMLHttpRequest) => {
                let msg = XMLHttpRequest.responseJSON;
                layer.msg(msg['msg'], {icon: 2});
            });
    });

    //获取复选框id
    function allChecked() {

        var id = [];
        var dom = $('.grid-view tbody').find('input[type=checkbox]:checked');
        if (dom.length) {
            $.each(dom, function () {
                id.push($(this).val())
            });
        } else {
            return 0;
        }
        return id;
    }

    <?php $this->endBlock() ?>
</script>
<?php $this->registerJs($this->blocks['js_ok_remove'], \yii\web\View::POS_END); ?>
